<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <!-- 角色管理模块 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole()"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :border="true" :data="RoleInfoList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="updateRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="page.pagesize"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- 角色管理模块 -->

          <!-- 公司信息模块 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="Companyform.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="Companyform.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="Companyform.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="Companyform.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 公司信息模块 -->
          <!-- 弹出层模块 -->
          <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
            <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="btnCancel">取消</el-button>
                <el-button size="small" type="primary" @click="btnOK">确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>
          <!-- 编辑弹窗 -->
          <el-dialog :visible="showPermDialog" @close="btnPermCancel">
            <!-- check-strictly 在显示复选框的情况下，是否严格的遵循父子不互相关联的做法，默认为 false	 -->
            <el-tree ref="permTree" :data="permList" :props="{label :'name'}" default-expand-all :show-checkbox="true" check-strictly node-key="id" />
            <template #footer>
              <el-row type="flex" justify="center">
                <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
                <el-button size="small" @click="btnPermCancel">取消</el-button>
              </el-row>
            </template>
          </el-dialog>
          <!-- 弹出层模块 -->
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>>

<script>
import { getCompanyInfo, getRoleInfo, deleteRole, updateRole, getRoleDetail, addRole, assignPermission } from '@/api/setting'
import { getPermissionList } from '@/api/permisson.js'
import { listToTreeData } from '@/utils/index'

export default {
  data() {
    return {
      RoleInfoList: [],
      Companyform: {},
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // 分页
      page: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      showDialog: false,
      showPermDialog: false,
      permList: [],
      roleId: ''
    }
  },
  computed: {
    title() {
      return this.roleForm.id ? '修改角色' : '新增角色'
    }
  },
  created() {
    // 获取公司信息
    this.getCompanyInfo()
    // 角色列表
    this.getRoleInfo()
    // 获得权限
    this.getPermission()
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      this.Companyform = await getCompanyInfo(
        this.$store.state.user.userInfo.companyId
      )
    },
    // 获取角色列表
    async getRoleInfo() {
      const { rows, total } = await getRoleInfo(this.page)
      this.total = total
      this.RoleInfoList = rows
    },
    // 翻页
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleInfo()
    },
    // 删除角色
    async  deleteRole(id) {
      try {
        await this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteRole(id)
        this.$message.success('删除成功')
        if (this.RoleInfoList.length === 1) {
          this.page.page--
        }
        this.getRoleInfo()
      } catch (error) {
        console.log(error)
      }
    },
    // 修改角色
    async  updateRole(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      this.roleForm = res
    },
    // 增加角色
    addRole() {
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    btnPermCancel() {
      this.showPermDialog = false
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
          // if (this.RoleInfoList.length === this.page.pagesize) {
          //   this.page.page++
          //   this.handleCurrentChange(this.page.page)
          // }
        }
        this.getRoleInfo()
        this.$message.success(`${this.title}成功`)
        this.showDialog = false
        this.roleForm = {
          name: '',
          description: ''
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑
    async assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.showPermDialog = true
      this.$nextTick(() => {
        console.log(permIds)
        // 通过 keys 设置目前勾选的节点，使用此方法必须设置 node-key 属性
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    async getPermission() {
      const res = await getPermissionList()
      console.log(res)
      this.permList = listToTreeData(res.filter(e => e.enVisible === '1'), '0')
    },
    async  btnPermOK() {
      const data = {
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      }
      console.log(data)
      // 发请求
      await assignPermission(data)
      // 提示用户
      this.$message.success('修改成功')
      // 关闭弹窗
      this.showPermDialog = false
      // console.log(this.$refs.permTree.getCheckedKeys())
    }
  }
}
</script>

<style>
</style>
